package sort;

/**
 * Created with IntelliJ IDEA.
 * Description: 希尔排序
 * User: Li_yizYa
 * Date: 2025/9/8
 * Time: 22:13
 */
public class ShellSort {
    public static void shellSort(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }
        int gap = nums.length / 2;
        while (gap >= 1) {
            shell(nums, gap);
            gap /= 2;
        }
    }
    public static void shell(int[] nums, int gap) {
        for (int i = gap; i < nums.length; i++) { // 待排序列
            int j = i - gap;
            int tmp = nums[i];
            for (; j >= 0; j -= gap) {
                if (nums[j] > tmp) {
                    nums[j + gap] = nums[j];
                } else {
                    break;
                }
            }
            nums[j + gap] = tmp;
        }
    }
    public static void main(String[] args) {
        int[] nums = {6,5,4,4,3,1,2,10,8};
        shellSort(nums);
        for (int num : nums) {
            System.out.print(num + " ");
        }
    }
}
